<?php
class HTMLPurifier_URIFilter_Hvn extends HTMLPurifier_URIFilter
{
    public $name = 'Hvn';
    //public $post = true;
    /*
    public function prepare($config) {
        //$this->target    = $config->get('URI', $this->name);
        //$this->parser    = new HTMLPurifier_URIParser();
    }
	*/
    
    public function filter(&$uri, $config, $context) {
        //only checks for img tags
        $token = $context->get('CurrentToken', true);
        if ($token->name !== 'img')
        	return true;
		//else continue to process the image
		//If image is on our site, we will return
		if (is_null($uri->host) || $uri->host == SITE_URL)
			return true; 

		$scheme_obj = $uri->getSchemeObj($config, $context);
        if (!$scheme_obj) 
        	return true; // ignore unknown schemes, maybe another postfilter did it
        if (empty($scheme_obj->browsable)) {
            return true;
        }
		$imgUrl = $uri->toString();
		
		//now get the image, save it to somewhere
		//die($imgUrl);
		//$newUrl = crawlImage($imgUrl);
		$crawler = new Utils_Crawler();
		//die_a($uri);
		$newLocalImgUrl = $crawler->processImage($imgUrl);
        $uri = new HTMLPurifier_URI(null, null, null, null, $newLocalImgUrl, null, null);
        //die_a($uri);
		return true;
    }
}
?>